package leetcode;

public class LC875Koko {

    public int getTime(int[] piles, int k) {
        int eatTime = 0;
        for (int i = 0; i < piles.length; i++) {
            eatTime += Math.ceil(piles[i] * 1.0 / k);
        }
        return eatTime;
    }

    public int binarySearch(int[] piles, int h, int left, int right) {
        while (left < right) {
            int mid = (left + right) / 2;
            if (getTime(piles,mid) > h) {
                left = mid + 1;
            } else right = mid;
        }
        return left;
    }

    public int minEatingSpeed(int[] piles, int h) {

        int kLeft = 1;
        int kRight = 1;
        int sum = 0;
        for (int i = 0; i < piles.length; i++) {
            if (piles[i] > kRight) kRight = piles[i];
            sum += piles[i];
        }
        kLeft = sum / h;
        return binarySearch(piles,h,kLeft,kRight);
    }


}
